#include "time.h"

void tim2Init(void)	//--用作指纹模块定时器
{
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
	TIM_OCInitTypeDef TIM_OCInitStruct;
	NVIC_InitTypeDef NVIC_InitStruct;

	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM2, ENABLE);// 开启时钟
	
	TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;// 定时器时钟 (CK_INT) 频率与死区发生器以及数字滤波器（ETR、TIx）所使用的死区及采样时钟 (tDTS) 之间的分频比
	TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;// 向上计数
	TIM_TimeBaseInitStruct.TIM_Period = 1000;// 定时100ms
	TIM_TimeBaseInitStruct.TIM_Prescaler = 16800-1;// 16800分频
	TIM_TimeBaseInit(TIM2, &TIM_TimeBaseInitStruct);

	TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_Timing;
	TIM_OC1Init(TIM2, &TIM_OCInitStruct);

	TIM_ITConfig(TIM2, TIM_IT_Update, ENABLE);
	TIM_ClearITPendingBit(TIM2, TIM_IT_Update);
	TIM_SetCounter(TIM2, 0);
	TIM_Cmd(TIM2, DISABLE);// 关闭定时器2
	
	NVIC_InitStruct.NVIC_IRQChannel = TIM2_IRQn;
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 2;
	NVIC_InitStruct.NVIC_IRQChannelSubPriority = 2;
	NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;
	NVIC_Init(&NVIC_InitStruct);// 配置中断管理器
}


void tim3Init(void)				//----自定义延时函数定时器
{
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM3, ENABLE);
	TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
	TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
	TIM_TimeBaseInitStruct.TIM_Period = 84-1;
	TIM_TimeBaseInitStruct.TIM_Prescaler = 0;
	
	TIM_TimeBaseInit(TIM3, &TIM_TimeBaseInitStruct);
}

void tim4Init(void)	//----------舵机，门锁PWM控制
{
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
	TIM_OCInitTypeDef TIM_OCInitStruct;
	GPIO_InitTypeDef GPIO_InitStruct;
	
	RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOD, ENABLE);
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM4,ENABLE);
	GPIO_InitStruct.GPIO_Pin = GPIO_Pin_12;
	GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
  	GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
 	GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
 	GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;

	GPIO_Init(GPIOD, &GPIO_InitStruct);
	GPIO_PinAFConfig(GPIOD,GPIO_PinSource12,GPIO_AF_TIM4);

	TIM_TimeBaseInitStruct.TIM_Period= 20000;   //自动装载值
	TIM_TimeBaseInitStruct.TIM_Prescaler=84 - 1; //分频系数
	TIM_TimeBaseInitStruct.TIM_ClockDivision=TIM_CKD_DIV1;
	TIM_TimeBaseInitStruct.TIM_CounterMode=TIM_CounterMode_Up; //设置向上计数模式
	TIM_TimeBaseInit(TIM4,&TIM_TimeBaseInitStruct);	
	
	TIM_OCInitStruct.TIM_OCIdleState = TIM_OCIdleState_Set;
	TIM_OCInitStruct.TIM_OCMode = TIM_OCMode_PWM1;
	TIM_OCInitStruct.TIM_OCPolarity = TIM_OCPolarity_High;
	TIM_OCInitStruct.TIM_OutputState = TIM_OutputState_Enable;
	TIM_OCInitStruct.TIM_Pulse = 0;
	TIM_OC1Init(TIM4, &TIM_OCInitStruct);
	
	TIM_OC1PreloadConfig(TIM4, TIM_OCPreload_Enable);
	TIM_ARRPreloadConfig(TIM4,ENABLE);//使能预装载寄存器

	TIM_CtrlPWMOutputs(TIM4, ENABLE);
	TIM_Cmd(TIM4,ENABLE); //使能定时器
}

void tim6Init(void)				//----按键复用判断定时器
{
	TIM_TimeBaseInitTypeDef TIM_TimeBaseInitStruct;
	NVIC_InitTypeDef  NVIC_InitStruct;
	RCC_APB1PeriphClockCmd(RCC_APB1Periph_TIM6, ENABLE);
	//基本定时器
	TIM_TimeBaseInitStruct.TIM_ClockDivision = TIM_CKD_DIV1;
	TIM_TimeBaseInitStruct.TIM_CounterMode = TIM_CounterMode_Up;
	TIM_TimeBaseInitStruct.TIM_Period = 10000;//装载值，计数次数,一个计时周期为10ms
	TIM_TimeBaseInitStruct.TIM_Prescaler = 84 -1;//预分频，基础频率为84MHz
	TIM_TimeBaseInit(TIM6, &TIM_TimeBaseInitStruct);//基本定时器初始化
	
	TIM_ITConfig(TIM6, TIM_IT_Update, ENABLE);	//设置中断源
	NVIC_InitStruct.NVIC_IRQChannel = TIM6_DAC_IRQn;//设置中断通道
	NVIC_InitStruct.NVIC_IRQChannelPreemptionPriority = 2;//抢占优先级
	NVIC_InitStruct.NVIC_IRQChannelSubPriority = 2;//响应优先级
	NVIC_InitStruct.NVIC_IRQChannelCmd = ENABLE;//中断使能
	NVIC_Init(&NVIC_InitStruct);//定时器中断初始化
}

void myDelayUs(u16 nus)
{
	u32 cnt;
	cnt = nus;
	TIM_Cmd(TIM3, ENABLE);
	while(cnt--)
	{
		while(TIM_GetFlagStatus(TIM3, TIM_FLAG_Update) == RESET);
		TIM_ClearFlag(TIM3, TIM_FLAG_Update);
	}
	TIM_Cmd(TIM3, DISABLE);
}

void myDelayMs(u16 nms)
{
	u32 cnt;
	cnt = nms*1000;
	TIM_Cmd(TIM3, ENABLE);
	while(cnt--)
	{
		while(TIM_GetFlagStatus(TIM3, TIM_FLAG_Update) == RESET);
		TIM_ClearFlag(TIM3, TIM_FLAG_Update);
	}
	TIM_Cmd(TIM3, DISABLE);
}


